package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.PlanContractQuantityRepository;
import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.model.enums.ContractStatDimensionEnum;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.sales_plan.contract.dto.BatchQueryContractByOrganiztionByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.contract.dto.BatchQueryContractByOrganiztionRequest;
import com.epoch.app.bcorder.sales_plan.contract.dto.PageQueryContractMoneyByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.contract.dto.PageQueryContractMoneyRequest;
import com.epoch.app.bcorder.sales_plan.contract.dto.StatisticalContractGroupByCompanyScItemByReadonlyRequest;
import com.epoch.app.bcorder.sales_plan.contract.dto.StatisticalContractGroupByCompanyScItemRequest;
import com.epoch.app.bcorder.sales_plan.contract.model.Contract;
import com.epoch.app.bcorder.sales_plan.contract.service.ContractService;
import com.epoch.app.otsapplicationreportsdk.contants.OtsApplicationReportSdkConstants;
import com.epoch.app.otsapplicationreportsdk.model.dto.BatchQueryContractQuantityRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/31 15:29
 */
@Component
public class PlanContractRepositoryImpl implements PlanContractQuantityRepository {

    @Resource
    ContractService contractService;

    /**
     * 批量查询汇总合同量，组织维度合同量
     * @param request
     * @return
     */
    @Override
    @RepositoryInvoker
    public List<Contract> batchQueryContractByOrganiztion(BatchQueryContractQuantityRequest request) {
        Result<List<Contract>> result = null;
        BatchQueryContractByOrganiztionRequest queryRequest = BatchQueryContractByOrganiztionRequest.builder()
                .contractYear(request.getContractYear())
                .dataSource(request.getDataSource())
                .build();
        if("103".equals(request.getType())){
            queryRequest.setType(ContractStatDimensionEnum.COMPANY_SC_ITEM.getCode());
            queryRequest.setOrgBusinessUnitCodes(distinctList(request.getOrganizationIds()));
        }else if("104".equals(request.getType())){
            queryRequest.setType(ContractStatDimensionEnum.SALES_ORGANIZATION.getCode());
            queryRequest.setOrgSalesOrganizationCodes(distinctList(request.getOrganizationIds()));
        }else if("105".equals(request.getType())){
            queryRequest.setType(ContractStatDimensionEnum.SALES_CHANNEL.getCode());
            queryRequest.setOrgSalesChannelCodes(distinctList(request.getOrganizationIds()));
        }else if("106".equals(request.getType())){
            queryRequest.setType(ContractStatDimensionEnum.DEPARTMENT.getCode());
            queryRequest.setOrgSalesDepartmentCodes(distinctList(request.getOrganizationIds()));
        }
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            BatchQueryContractByOrganiztionByReadonlyRequest queryReadonlyRequest = new BatchQueryContractByOrganiztionByReadonlyRequest();
            BeanUtils.copyProperties(queryRequest,queryReadonlyRequest);
            result = contractService.batchQueryContractByOrganiztionByReadonly(queryReadonlyRequest);
        }else{
            result = contractService.batchQueryContractByOrganiztion(queryRequest);
        }
        return result.getResult();
    }

    /**
     * 分页查询客户送达方x货品合同金额，data_source = '0' excel导入
     * @param request
     * @return
     */
    @Override
    public Result<List<Contract>> pageQueryContractMoney(BatchQueryContractQuantityRequest request) {
        Result<List<Contract>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            PageQueryContractMoneyByReadonlyRequest queryRequest = PageQueryContractMoneyByReadonlyRequest.builder()
                    .contractYear(request.getContractYear())
                    .orgBusinessUnitCode(request.getOrgBusinessUnitCode())
                    .customerCode(request.getCustomerCode())
                    .customerAddressCode(request.getCustomerAddressCode())
                    .scItemId(request.getScItemId())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = contractService.pageQueryContractMoneyByReadonly(queryRequest);
        }else{
            PageQueryContractMoneyRequest queryRequest = PageQueryContractMoneyRequest.builder()
                    .contractYear(request.getContractYear())
                    .orgBusinessUnitCode(request.getOrgBusinessUnitCode())
                    .customerCode(request.getCustomerCode())
                    .customerAddressCode(request.getCustomerAddressCode())
                    .scItemId(request.getScItemId())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = contractService.pageQueryContractMoney(queryRequest);
        }
        return result;
    }

    @Override
    public Result<List<Contract>> statisticalContractGroupByCompanyScItem(BatchQueryContractQuantityRequest request){
        Result<List<Contract>> result = null;
        if("1".equals(OtsApplicationReportSdkConstants.QUERY_READONLY_DATABASE())){
            StatisticalContractGroupByCompanyScItemByReadonlyRequest queryRequest = StatisticalContractGroupByCompanyScItemByReadonlyRequest.builder()
                    .contractYear(request.getContractYear())
                    .type(request.getType())
                    .orgBusinessUnitCodes(request.getOrganizationIds())
                    .dataSource(request.getDataSource())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = contractService.statisticalContractGroupByCompanyScItemByReadonly(queryRequest);
        }else{
            StatisticalContractGroupByCompanyScItemRequest queryRequest = StatisticalContractGroupByCompanyScItemRequest.builder()
                    .contractYear(request.getContractYear())
                    .type(request.getType())
                    .orgBusinessUnitCodes(request.getOrganizationIds())
                    .dataSource(request.getDataSource())
                    .start(request.getStart())
                    .limit(request.getLimit())
                    .build();
            result = contractService.statisticalContractGroupByCompanyScItem(queryRequest);
        }
        return result;
    }

    private List<String> distinctList(List<String> ids){
        Set set = new HashSet();
        set.addAll(ids);
        ids.clear();
        ids.addAll(set);
        return ids;
    }
}
